package x10.enums;

import java.util.EnumSet;
import java.util.HashMap;

/**
 * Security - enumeration of security commands and security sensor elements
 * @author Denny Sheirer
 * @date 20110126
 */
public enum Security {
    //signed int in parenthesis represents the byte value used for each command
    //in the protocol
    DELAYED_ARM_AWAY(2),
    ARM_AWAY(6),
    ARM_HOME(14),
    PANIC(34),
    SECURITY_LIGHTS_ON(66),
    DISARM(-126),
    SECURITY_LIGHTS_OFF(-62),

    //int in parenthesis for below security sensor elements is arbitrary
    //for consistency with above.  I just chose 100 as a starting point
    BATTERY_LOW(100),
    BATTERY_GOOD(101),

    DELAY_MIN(102),
    DELAY_MAX(103),

    CONTACT_OPEN(104),
    CONTACT_CLOSED(105),

    MOTION_DETECTED(106),
    NO_MOTION(107),

    UNKNOWN(108);

    //hex code representation converted to an int
    private final int code;

    //Lookups to convert from integer to Status code
    private static final HashMap<Integer,Security> lookup = new HashMap<Integer,Security>();

    /**
     * Hidden class to populate the lookup hashmap
     */
    static {
        for(Security s : EnumSet.allOf(Security.class)){
            lookup.put(s.code, s);
        }
    }

    /**
     * Constructor
     * @param code - int value of the hex code equivalent for the security command
     */
    Security(int code){
        this.code = code;
    }

    /**
     * Utility method to get the byte equivalent of the int value for the command
     * @param code - int value of the byte representing the command
     * @return
     */
    public byte getByteValue(){
        return (byte) this.code;
    }

    /**
     * Utility method to get a security command object from the security command
     * @param statusCode - int value of the security command byte
     * @return - Security object
     */
    public static Security getSecurity(int statusCode){
        return lookup.get(statusCode);
    }
}
